<!--
 * 严肃声明：
 * 开源版本请务必保留此注释头信息，若删除我方将保留所有法律责任追究！
 * 本系统已申请软件著作权，受国家版权局知识产权以及国家计算机软件著作权保护！
 * 可正常分享和学习源码，不得用于违法犯罪活动，违者必究！
 * Copyright (c) 2020 陈尼克 all rights reserved.
 * 版权所有，侵权必究！
 *
-->

<template>
  <div class="login">
    <s-header :name="type == 'login' ? '登录' : '注册'" :back="'/home'"></s-header>
    <img class="logo" src="//s.weituibao.com/1582958061265/mlogo.png" alt="">
    <div v-if="type == 'login'" class="login-body login">
      <!-- 多方式登入 -->
      <van-form>
        <van-field
          v-model="username"
          name="username"
          label="用户名"
          placeholder="用户名"
          :rules="[{ required: true, message: '请填写用户名' }]"
        />
        <van-field
          v-model="password"
          type="password"
          name="password"
          label="密码"
          placeholder="密码"
          :rules="[{ required: true, message: '请填写密码' }]"
        />
        <div style="margin: 16px;">
          <div class="sms_login" @click="toggle('sms_login')">短信登入</div>
          <div class="link-register" @click="toggle('register')">立即注册</div>
          <van-button round block type="info" color="#1baeae" @click="ClickMulLogin">登录</van-button>
        </div>
      </van-form>
    </div>

    <div v-else-if="type == 'sms_login'" class="login-body login">
      <van-form>
                <!-- 短信登入 -->
       <van-cell-group>
        <van-field
          v-model="mobile1"
          name="mobile1"
          label="手机号"
          placeholder="手机号"
          :rules="[{ required: true, message: '请填写手机号' }]"
          @blur="check_mobile1"
        />
          <van-field
            v-model="code1"
            name="code1"
            center
            clearable
            label="短信验证码"
            placeholder="请输入短信验证码"
          >
            <template #button>
              <van-button size="small" type="primary" color="#1baeae" @click="send_sms">{{sms_interval}}</van-button>
            </template>
          </van-field>
        </van-cell-group>
      <div style="margin: 16px;">
          <div class="sms_login" @click="toggle('login')">密码登入</div>
          <div class="link-register" @click="toggle('register')">立即注册</div>
          <van-button round block type="info" color="#1baeae" @click="ClickSmsLogin">登录</van-button>
      </div>
      </van-form>
    </div>
    
    <div v-else class="login-body register">
      <!-- 短信注册 -->
      <van-form>
        <van-field
          v-model="mobile"
          name="mobile"
          label="手机号"
          placeholder="手机号"
          :rules="[{ required: true, message: '请填写手机号' }]"
          @blur="check_mobile"
        />
        <van-field
          v-model="password1"
          type="password"
          name="password1"
          label="密码"
          placeholder="密码"
          :rules="[{ required: true, message: '请填写密码' }]"
        />

        <van-field
        v-model="code"
        name="code"
        center
        clearable
        label="短信验证码"
        placeholder="请输入短信验证码"
        :rules="[{ required: true, message: '请填写短信验证码' }]"
      >

        <template #button>
          <van-button size="small" type="primary" color="#1baeae" @click="send_sms" round>{{sms_interval}}</van-button>
        </template>

      </van-field>

        <div style="margin: 16px;">
          <div class="link-login" @click="toggle('login')">已有登录账号</div>
          <van-button round block type="info" color="#1baeae" native-type="submit" @click="ClickRegister">注册</van-button>
        </div>

      </van-form>
    </div>

  </div>
</template>

<script>
import sHeader from '@/components/SimpleHeader'
import { setLocal, getLocal } from '@/common/js/utils'
import { Toast } from 'vant'

export default {
  data() {
    return {
      username: '',
      password: '',
      mobile: '',
      mobile1: '',
      code1:'',
      password1: '',
      code:'',
      type: 'login',
      verify: false,
      sms_interval:'发送验证码',
      is_send:false
    }
  },
  components: {
    sHeader,

  },
  methods: {
    toggle(v) {
      this.verify = false
      this.type = v
    },
    check_mobile(){
      if (!this.mobile) return;
      if (!this.mobile.match(/^1[3-9][0-9]{9}$/)) {
        Toast.fail('手机号有误')
        return false;
      }
      this.$axios.get(this.$settings.BASE_URL + 'userinfo/user/mobile/?mobile=' + this.mobile).then(res => {
        if (res.code == 100) {
          this.mobile = ''
          Toast.fail('手机号已注册，请登入')
          return  // 函数结束掉
        } else {
          this.is_send = true;
        }
      })
    },
    check_mobile1(){
      if (!this.mobile1) return;
      if (!this.mobile1.match(/^1[3-9][0-9]{9}$/)) {
        Toast.fail('手机号有误')
        return false;
      }
      this.$axios.get(this.$settings.BASE_URL + 'userinfo/user/mobile/?mobile=' + this.mobile1).then(res => {
        if (res.code !== 100) {
          this.mobile1 = ''
          Toast.fail('手机号没有注册，请先注册')
          return  // 函数结束掉
        } else {
          this.is_send = true;
        }
      })
    },
    send_sms() {
      if (!this.is_send) return;
      this.is_send = false;
      let sms_interval_time = 60;
      this.sms_interval = "发送中...";
      let timer = setInterval(() => {
        if (sms_interval_time <= 1) {
          clearInterval(timer);
          this.sms_interval = "获取验证码";
          this.is_send = true; // 重新回复点击发送功能的条件
        } else {
          sms_interval_time -= 1;
          this.sms_interval = `${sms_interval_time}秒后再发`;
        }
      }, 1000);
      this.$axios.get(this.$settings.BASE_URL + 'userinfo/user/send_sms/?mobile=' + this.mobile).then(res => {
        if (res.code !== 100) {
          Toast.fail(res.msg)
        }
      })
    },
    ClickMulLogin(){
      this.$axios.post(this.$settings.BASE_URL + 'userinfo/user/mul_login/',{
          username:this.username,password:this.password}
          ).then(res=>{
            if(res.code==100){
              this.$cookies.set('token', res.token, '7d')
              this.$cookies.set('name', res.username, '7d')
              this.$cookies.set('icon', res.icon, '7d')
              this.$cookies.set('id', res.id, '7d')
              this.$cookies.set('mobile', res.mobile, '7d')
              window.location.href = '/'
            }else{
              Toast.fail(res.msg)
            }        
        })
    },
    ClickRegister(){
      this.$axios.post(this.$settings.BASE_URL + 'userinfo/register/mul_register/',{
            mobile:this.mobile,password:this.password1,code:this.code
          }).then(res=>{
            if(res.code==100){
              Toast.success('注册成功')
              this.$cookies.set('token', res.token, '7d')
              this.$cookies.set('name', res.username, '7d')
              this.$cookies.set('icon', res.icon, '7d')
              this.$cookies.set('id', res.id, '7d')
              this.$cookies.set('mobile', res.mobile, '7d')
              window.location.href = '/'
            }else{
              Toast.fail(res.msg)
            }
          })
    },
    ClickSmsLogin(){
      this.$axios.post(this.$settings.BASE_URL + 'userinfo/user/mobile_login/',{
          mobile:this.mobile1,code:this.code1}
          ).then(res=>{
            if(res.code==100){
              this.$cookies.set('token', res.token, '7d')
              this.$cookies.set('name', res.username, '7d')
              this.$cookies.set('icon', res.icon, '7d')
              this.$cookies.set('id', res.id, '7d')
              this.$cookies.set('mobile', res.mobile, '7d')
              window.location.href = '/'
            }else{
              Toast.fail(res.msg)
            }
        })
    }
  },
}
</script>

<style lang="less">
  .login {
    .logo {
      width: 120px;
      height: 120px;
      display: block;
      margin: 80px auto 0px;
    }
    .login-body {
      padding: 0 20px;
    }
    .login {
      .link-register {
        float: right;
        font-size: 14px;
        margin-bottom: 20px;
        color: #1989fa;
        display: inline-block;
      }
    }
    .login{
      .sms_login {
        font-size: 14px;
        margin-bottom: 20px;
        color: #1989fa;
        display: inline-block;
      }
    }
    .register {
      .link-login {
        font-size: 14px;
        margin-bottom: 20px;
        color: #1989fa;
        display: inline-block;
      }
    }
    .verify-bar-area {
      margin-top: 24px;
      .verify-left-bar {
        border-color: #1baeae;
      }
      .verify-move-block {
        background-color: #1baeae;
        color: #fff;
      }
    }
    .verify {
      >div {
        width: 100%;
      }
      display: flex;
      justify-content: center;
      .cerify-code-panel {
        margin-top: 16px;
      }
      .verify-code {
        width: 40%!important;
        float: left!important;
      }
      .verify-code-area {
        float: left!important;
        width: 54%!important;
        margin-left: 14px!important;
        .varify-input-code {
          width: 90px;
          height: 38px!important;
          border: 1px solid #e9e9e9;
          padding-left: 10px;
          font-size: 16px;
        }
        .verify-change-area {
          line-height: 44px;
        }
      }
    }
  }
</style>
